package com.emuporium.gymrat.server.routine;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import com.emuporium.gymrat.server.data.AjaxResponse;
import com.emuporium.gymrat.server.data.ExerciseDB;
import com.emuporium.gymrat.server.data.User;
import com.emuporium.gymrat.server.data.UserDB;
import com.emuporium.gymrat.server.data.Variation;

@SuppressWarnings("serial")
public class RoutineAddEntryAjaxServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		int exerciseId = new Integer(request.getParameter("exerciseid"));

		AjaxResponse varResponse = new AjaxResponse();
		User user = UserDB.selectUser(request.getRemoteUser());

		ArrayList<Variation> variations = ExerciseDB
				.selectVariations(exerciseId,user.getId());
		
		for(Variation v: variations){
			varResponse.add(v.getId(), v.getName());
		}
//		varResponse.addMap(ExerciseDB.selectVariations(exerciseId));

		Serializer serializer = new Persister();
		ByteArrayOutputStream stream = new ByteArrayOutputStream();

		// Write the AjaxResponse object as XML
		try {
			serializer.write(varResponse, stream);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		response.setContentType("text/xml");
		response.getWriter().write(stream.toString());
	}
}
